<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Category;
use App\Models\Project;
use Illuminate\Http\Request;

class ProjectController extends BaseController
{
    public $model = Project::class;


    public function index(Request $request) {
        $input = $request->input();
        $search = ['on_status' => -1, 'start_at' => '', 'end_at' => '', 'category_id' => 0];
        $name = $request->input('name');
        $amount = $request->input('amount');
        $company_title = $request->input('company_title');
        $company_type = $request->input('company_type');
        $tag_ids = $request->input('tag_ids');
        $list = Project::withCertain('category', ['title'])
            ->withCertain('parent_category', ['title'])
            ->apply($search, $input)
            ->when($name, function ($q) use ($name) {
                $q->where('name', 'like', '%' . $name . '%');
            })
            ->when($amount, function ($q) use ($amount) {
                $q->where('amount', $amount);
            })
            ->when($company_title, function ($q) use ($company_title) {
                $q->where('company_title', 'like', '%' . $company_title . '%');
            })
            ->when($company_type, function ($q) use ($company_type) {
                $q->where('company_type', $company_type);
            })
            ->when($tag_ids, function ($q) use ($tag_ids) {
                if ($tag_ids == 1)
                    $q->where('is_special', 1);
                else if($tag_ids == 2)
                    $q->where('is_rec', 1);
                else if($tag_ids == 3)
                    $q->where('is_brand_news', 1);

            })
            ->orderBy("sort", "desc")->orderBy("pv", "desc")->orderBy("id", "desc")
            ->paginate(per_page());
        foreach ($list as $item) {
            $item->append(['tag_ids_show']);
        }
        return $this->paginate($list);
    }


    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request) {
        $input = $request->input();
        $id = \request("id");

        $category = Category::find($input['category_id']);
        if (!$category)
            return $this->failed('分类id不存在');
        $input['parent_category_id'] = $category->parent_id;
        $tag_ids = $input['tag_ids'] ?? null;
        $arr_tag_ids = json_decode($tag_ids, true);
        if (in_array('1', $arr_tag_ids)) {
            $input['is_special'] = 1;
        } else {
            $input['is_special'] = 0;
        }
        if (in_array('2', $arr_tag_ids)) {
            $input['is_rec'] = 1;
        } else {
            $input['is_rec'] = 0;
        }
        if (in_array('3', $arr_tag_ids)) {
            $input['is_brand_news'] = 1;
        } else {
            $input['is_brand_news'] = 0;
        }
        if ($id) {
            $model = Project::find($id);
            $model->update($input);
        } else {
            $info = Project::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'ids' => 'required'
        ]);
        $ids = $request->input('ids');
        $arr_id = explode(",", $ids);
        foreach ($arr_id as $id) {
            $model = Project::find ($id);
            $model->delete();
        }
        return $this->success([]);
    }

    public function info(Request $request)
    {
        $model = new $this->model;
        $table = $model->getTable();
        $this->validate($request, [
            'id' => 'required|exists:' . $table . ",id"
        ]);
        $id = \request("id");
        $info = $this->model::find($id);
        return $this->success($info);
    }

}

